/************************************************************
wx
2020/1/17

*************************************************************/

using System.Collections.Generic;
using UnityEngine;

namespace Fight
{
    public class Round
    {
        //private Queue<RoundState> m_queRoundState;
        public Character RoundOwner { get; private set; }
        public Character RoundEnemy { get; private set; }

        public Round(Character owner,Character enemy)
        {
            RoundOwner = owner;
            RoundEnemy = enemy;
        }

        public void RoundStart()
        {
            PassTime = 0;
            PlayTime = 0;
            //CurrentState = RoundState.Settle;
            MonoEvent.Instance.UPDATE += OnUpdate;
            Debug.Log($"{RoundOwner.Name}回合开始");
            Next();
        }

        public void RoundEnd()
        {
            PassTime = 0;
            CurrentState = RoundState.End;
            MonoEvent.Instance.UPDATE -= OnUpdate;
        }

        public RoundState CurrentState { get; private set; } = RoundState.PreStart;

        /// <summary>
        /// 获取回合持续了多长时间
        /// </summary>
        public float PassTime { get; private set; } = 0;

        /// <summary>
        /// 获取出牌阶段用了多长时间
        /// </summary>
        public float PlayTime { get; private set; } = 0;

        /*
        private void InitRoundQueue()
        {
            if (m_queRoundState == null)
            {
                m_queRoundState = new Queue<RoundState>();
            }
            else
            {
                m_queRoundState.Clear();
            }

            m_queRoundState.Enqueue(RoundState.Settle);
            m_queRoundState.Enqueue(RoundState.Draw);
            m_queRoundState.Enqueue(RoundState.Play);
            m_queRoundState.Enqueue(RoundState.End);
        }*/

        public void Next()
        {
            switch (CurrentState)
            {
                case RoundState.PreStart:
                    {
                        CurrentState = RoundState.Settle;
                        StateHelper helper = new StateHelper(RoundOwner);
                        helper.HandleStatus();
                        MessageManager.Instance.Excute(MessageType.RoundSettle);

                        

                        //开始自动过渡到结算阶段
                        Next();
                    }
                    break;
                case RoundState.Settle:
                    {
                        RoundOwner.MpChange(RoundOwner.MpMax);
                        CurrentState = RoundState.Draw;
                        List<Card> listCards = RoundOwner.DrawCards();
                        MessageManager.Instance.Excute(MessageType.RoundDraw, listCards);

                        Debug.Log($"{RoundOwner.Name}结算状态");
                        //结算阶段自动过渡到抽卡阶段
                        Next();
                    }
                    break;
                case RoundState.Draw:
                    {
                        Debug.Log($"{RoundOwner.Name}开始抽卡");
                        CurrentState = RoundState.Play;
                        MessageManager.Instance.Excute(MessageType.RoundPlay);

                        if (RoundOwner.Type == CharacterType.Monster)
                        {
                            Next();
                        }
                    }
                    break;
                case RoundState.Play:
                    {
                        //怪物自动出牌
                        if (RoundOwner.Type == CharacterType.Monster)
                        {
                            RoundOwner.UseCard(RoundEnemy);
                        }
                    }
                    break;
                case RoundState.End:
                    {

                    }
                    break;
            }
        }

        /// <summary>
        /// 回合出卡
        /// </summary>
        public bool Play()
        {
            if (CurrentState == RoundState.Play)
            {
                return RoundOwner.UseCard(RoundEnemy);
            }

            return false;
        }

        //每帧调用
        private void OnUpdate()
        {
            PassTime += Time.deltaTime;
            if (CurrentState == RoundState.Play)
            {
                PlayTime += Time.deltaTime;
            }
            else
            {
                PlayTime = 0;
            }
        }
    }

    public enum RoundState
    {
        PreStart,   //未开始
        Settle, //结算buff
        Draw,   //抽卡
        Play,   //出卡
        End,
    }
}

